package com.webside.points.wechat;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginFilter implements Filter{

	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)req;
		HttpServletResponse response = (HttpServletResponse) res;
		
		String requestUrl = request.getRequestURI();
		//System.out.println("当前请求路径：>>>"+requestUrl);
		if(requestUrl.contains("/findAll")&&requestUrl.contains("/findAllUI")){
			String userName = (String) request.getSession().getAttribute("userName");
			//System.out.println("当前登录用户：>>>"+userName);
			if(userName!=null){
				chain.doFilter(req, res);
			}else{
				 PrintWriter out = response.getWriter();  
			        out.println("<html>");      
			        out.println("<script>");      
			        out.println("window.open ('../serve/login.jsp','_top')");      
			        out.println("</script>");      
			        out.println("</html>");    
			}
		}else{
			chain.doFilter(req, res);
		}
		
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
